package com.hidtechs.popfax.view;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hidtechs.popfax.R;
import com.hidtechs.popfax.controller.Constants;
import com.hidtechs.popfax.controller.Requestor;
import com.hidtechs.popfax.xml.ParsedExampleDataSet;

public class Login extends Activity implements Runnable,OnClickListener{
    /** Called when the activity is first created. */
	
	Button btnSignIn;
	Character class_type='l';
	EditText userName;
	EditText password;
	
	String  PREFS="preferences";
	protected Handler handler;
	private ProgressDialog progressDialog ;
	protected HttpURLConnection conn=null;
	public HashMap<String,String> parameterValues;
	protected ArrayList arrayOfObjectOfParsedData;
	public static  String userName_shared_prefernece=null;
	public  static String passWord_shared_prefernece=null;

	
	View view=null;

	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        userName=(EditText)findViewById(R.id.userName);
		password=(EditText)findViewById(R.id.password);
		btnSignIn=(Button)findViewById(R.id.btnSighIn);
		btnSignIn.setOnClickListener(this);
	}
    


	public void onClick( View v) {
    	
		 String UserName = userName.getText().toString();
         String PassWord = password.getText().toString();                 
         if (UserName.length()==0 || PassWord.length()==0)                
         {
             Toast.makeText(getBaseContext(), 
                Constants.EMPLTY_LOGIN_FIELDS_DATA, 
                 Toast.LENGTH_SHORT).show();
         }else
         {
        	 view = v;
        	 retrievedata();
         }
		   
        
	}
    
	protected void retrievedata()
	{
		removePreference(); // remove preferneces if any thing already there
    	
    	
    	
    	//savePreference(userName.getText().toString(),password.getText().toString()); 
    	savePreference("mah.btech@gmail.com","e54vwjuf");
    	setMapValues();
    	
    	progressDialog = ProgressDialog.show(this,Constants.DIALOG_WAIT_MSG,Constants.LOGIN_DIALOG_AUTHENTICATION_MSG, true,false);
    
    	 	
    	Thread thread = new Thread(this);
        thread.start(); //invoke the run() of Base class
        
        loginHandlerMethod();
    	
	}
	
	
	public void run() {
		System.out.println("In Run");
		
		setMapValues();
    	Requestor requestorForXMLParsing= new Requestor();
		arrayOfObjectOfParsedData=requestorForXMLParsing.requestData(class_type,parameterValues);
		
		handler.sendEmptyMessage(0);		
	}
	
	
	public void loginHandlerMethod()
	{
		handler = new Handler() {

 		public void handleMessage(Message msg) {
 			progressDialog.dismiss();

 			System.out.println("Handler");
 				 if(((ParsedExampleDataSet)(arrayOfObjectOfParsedData.get(0))).getResult()==Constants.LOGIN_SUCCESS_CODE)
 				 {
 			 Toast.makeText(getApplicationContext(),Constants.LOGGED_IN_MSG, Toast.LENGTH_SHORT).show();
 			 Intent tabIntent = new Intent(view.getContext(),com.hidtechs.popfax.view.TabWidgetController.class);
 				 startActivity(tabIntent);
 				 }
 				 if(((ParsedExampleDataSet)(arrayOfObjectOfParsedData.get(0))).getResult()==Constants.LOGIN_FAILURE_CODE)
     			 {
     				 Toast.makeText(getApplicationContext(),Constants.LOGGED_IN_FAILED_MSG, Toast.LENGTH_SHORT).show();
     			 }
 			 
 		}
 		
 	};

	}
	
	void setMapValues() {

		loadPreference(); 
		parameterValues = new HashMap();
		parameterValues.put(Constants.URL_ACTION,Constants.LOGIN_ACTION);
		parameterValues.put(Constants.URL_USERNAME,userName_shared_prefernece);
		parameterValues.put(Constants.URL_PASSWORD,passWord_shared_prefernece);
		
	}


		
	protected void savePreference(String username,String password)
	{

		int mode = Activity.MODE_PRIVATE;
		SharedPreferences mySharedPreferences = getSharedPreferences(PREFS, mode);
		SharedPreferences.Editor editor = 
		mySharedPreferences.edit();
		System.out.println("savepreference="+username+password);
		String userName = username;
		String passWord = password;
		editor.putString("USER_NAME",userName );
		editor.putString("PASS_WORD",password );
		editor.commit();
	}


	protected void loadPreference()
	{
		int mode = Activity.MODE_PRIVATE;
		SharedPreferences mySharedPreferences = getSharedPreferences(PREFS, mode);
		userName_shared_prefernece= mySharedPreferences.getString("USER_NAME","UserName");
		System.out.println(userName_shared_prefernece);
		passWord_shared_prefernece = mySharedPreferences.getString("PASS_WORD","Password");
		System.out.println(passWord_shared_prefernece);
	}
    
    
	protected void removePreference() {
		int mode = Activity.MODE_PRIVATE;
		SharedPreferences mySharedPreferences = getSharedPreferences(PREFS, mode);
		 
		SharedPreferences.Editor editor = mySharedPreferences.edit(); 
				  editor.remove("USER_NAME"); 
				             editor.remove("PASS_WORD"); 
				             editor.commit(); 
	}




}